package edu.jmu.action.sysmanage.log;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import edu.jmu.action.BaseAction;
import edu.jmu.action.privilege.Permission;
import edu.jmu.bean.PageView;
import edu.jmu.bean.system.LoginLog;
import edu.jmu.service.sysmanage.LoginLogService;

@Controller
@Scope(value = "prototype")
public class LoginLogManageAction extends BaseAction<LoginLog> {
	private static final long serialVersionUID = -4680298702412042355L;
 
	@Resource
	LoginLogService loginLogService;
	private String startDate;
	private String endDate;
	private Integer[] delId;

	@Permission(module="sysmanage",privilege="listLoginLog",desc="展示登陆日志列表")
	public String listLoginLog() {
		pageView = new PageView<LoginLog>(maxresult, currentpage);
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		if (startDate != null || endDate != null ) {
			if( !"".equals(startDate) || !"".equals(endDate)){
				String wherejpql = "o.loginTime >=?1 and o.loginTime <=?2";
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				try {
					pageView.setQueryResult(loginLogService.getScrollData(
							pageView.getFirstResult(), pageView.getMaxresult(),
							wherejpql, new Date[] { dateFormat.parse(startDate),
									dateFormat.parse(endDate) }, orderby));
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}else{
				pageView.setQueryResult(loginLogService.getScrollData(pageView.getFirstResult(), pageView.getMaxresult(), orderby));
			}
		} else {
			pageView.setQueryResult(loginLogService.getScrollData(pageView.getFirstResult(), pageView.getMaxresult(), orderby));
		}
		return "success";
	}

	@Permission(module="sysmanage",privilege="deleteLoginLog",desc="删除登陆日志")
	public String deleteOperate(){
		loginLogService.delete((Serializable[])delId);
		return SUCCESS;
	}
	
	/** set */
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getStartDate() {
		return startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setDelId(Integer[] delId) {
		this.delId = delId;
	}
	

}
